package com.scoy.honeymei.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scoy.honeymei.R;
import com.scoy.honeymei.bean.CartBean;
import com.scoy.honeymei.bean.GoodsOrderBean;
import com.scoy.honeymei.custem.LeftSwipeLayout;
import com.scoy.honeymei.utils.GlideImgUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.media.CamcorderProfile.get;

/**
 * @ClassName: NormalAdapter$
 * @Description:
 * @Author: GaoZhan
 * @CreateDate: 2019/8/26/0026$ 下午 17:03$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/8/26/0026$ 下午 17:03$
 * @UpdateRemark: 更新内容
 */
public class CartAdapter extends RecyclerView.Adapter {
    private Context context;
    private ArrayList<CartBean> datalist;
    private TextView notv;

    public CartAdapter(Context context, ArrayList<CartBean> datalist, TextView notv) {
        this.context = context;
        this.datalist = datalist;
        this.notv = notv;
    }

    public interface OnOneClick {
        void oneClick(int pos);
    }

    OnOneClick onOneClick;

    public void setOnOneClick(OnOneClick onOneClick) {
        this.onOneClick = onOneClick;
    }

    public interface OnTwoClick {
        void twoClick(int pos, int posin);
    }

    OnTwoClick onTwoClick;

    public void setOnTwoClick(OnTwoClick onTwoClick) {
        this.onTwoClick = onTwoClick;
    }

    public interface OnThreeClick {
        void threeClick(int pos, int posin);
    }

    OnThreeClick onThreeClick;

    public void setOnThreeClick(OnThreeClick onThreeClick) {
        this.onThreeClick = onThreeClick;
    }

    public interface OnFourClick {
        void fourClick(int pos, int posin);
    }

    OnFourClick onFourClick;

    public void setOnFourClick(OnFourClick onFourClick) {
        this.onFourClick = onFourClick;
    }

    public interface OnFiveClick {
        void fiveClick(int pos, int posin);
    }

    OnFiveClick onFiveClick;

    public void setOnFiveClick(OnFiveClick onFiveClick) {
        this.onFiveClick = onFiveClick;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_cart, parent, false);
        return new CartHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull List payloads) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else {
            CartHolder cartHolder = (CartHolder) holder;
            CartBean cartBean = datalist.get(position);
            List<GoodsOrderBean> goodsList = cartBean.getGoodsList();
            RecyclerView.Adapter adapter = cartHolder.icaRv.getAdapter();

            String loads = (String) payloads.get(0);

            //子view触发
            if ("checkshop".equals(loads)) {
                GlideImgUtil.glidePic(context, R.drawable.ic_checked_cart, cartHolder.icaCheckIv);
            } else if ("uncheckshop".equals(loads)) {
                GlideImgUtil.glidePic(context, R.drawable.ic_uncheck_cart, cartHolder.icaCheckIv);
            }

            //主view触发
            if ("checkshop0".equals(loads)) {
                GlideImgUtil.glidePic(context, R.drawable.ic_checked_cart, cartHolder.icaCheckIv);
                for (int i = 0; i < goodsList.size(); i++) {
                    adapter.notifyItemChanged(i, "checkgoods");
                }
            } else if ("uncheckshop0".equals(loads)) {
                GlideImgUtil.glidePic(context, R.drawable.ic_uncheck_cart, cartHolder.icaCheckIv);
                for (int i = 0; i < goodsList.size(); i++) {
                    adapter.notifyItemChanged(i, "uncheckgoods");
                }

            }

        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        CartHolder cartHolder = (CartHolder) holder;
        CartBean cartBean = datalist.get(position);
        cartHolder.icaShopnameTv.setText(cartBean.getShopname());
        List<GoodsOrderBean> goodsList = cartBean.getGoodsList();

/*        ArrayList<GoodsOrderBean> dataItemList = new ArrayList<>();
        dataItemList.addAll(goodsList);
        CartItemAdapter cartItemAdapter = new CartItemAdapter(context, dataItemList);
        cartHolder.icaRv.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
        DividerItemDecoration did = new DividerItemDecoration(context, DividerItemDecoration.VERTICAL);
        did.setDrawable(context.getResources().getDrawable(R.drawable.divider_gray_line_pd));
        cartHolder.icaRv.addItemDecoration(did);
        cartHolder.icaRv.addOnItemTouchListener(new LeftSwipeLayout.OnSwipeItemTouchListener(context));
        cartHolder.icaRv.setAdapter(cartItemAdapter);*/

        int shopselect = cartBean.getShopselect();
        if(shopselect==1){
            GlideImgUtil.glidePic(context, R.drawable.ic_checked_cart, cartHolder.icaCheckIv);
        }else {
            GlideImgUtil.glidePic(context, R.drawable.ic_uncheck_cart, cartHolder.icaCheckIv);
        }

        cartHolder.icaCheckIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onOneClick != null) {
                    onOneClick.oneClick(position);
                }
            }
        });

       /* cartItemAdapter.setOnOneClick(new CartItemAdapter.OnOneClick() {  //jian
            @Override
            public void oneClick(int pos) {
                if (onTwoClick != null) {
                    onTwoClick.twoClick(position, pos);
                }
            }
        });

        cartItemAdapter.setOnTwoClick(new CartItemAdapter.OnTwoClick() { //add
            @Override
            public void twoClick(int pos) {
                if (onThreeClick != null) {
                    onThreeClick.threeClick(position, pos);
                }
            }
        });

        cartItemAdapter.setOnThreeClick(new CartItemAdapter.OnThreeClick() {  //selsect
            @Override
            public void threeClick(int pos) {
                if (onFourClick != null) {
                    List<GoodsOrderBean> goodsList1 = datalist.get(position).getGoodsList();
                    GoodsOrderBean goodsOrderBean = goodsList1.get(pos);
                    if (goodsOrderBean.getGoodsselect() == 1) {
                        goodsOrderBean.setGoodsselect(0);
                        cartItemAdapter.notifyItemChanged(pos, "uncheckgoods");
                    } else {
                        goodsOrderBean.setGoodsselect(1);
                        cartItemAdapter.notifyItemChanged(pos, "checkgoods");
                    }
                    onFourClick.fourClick(position, pos);
                }
            }
        });

        cartItemAdapter.setOnFourClick(new CartItemAdapter.OnFourClick() { //delete
            @Override
            public void fourClick(int pos) {
                if (onFiveClick != null) {
                    onFiveClick.fiveClick(position, pos);
                }
            }
        });*/


    }

    @Override
    public int getItemCount() {
        int iCount = datalist == null ? 0 : datalist.size();
        if (iCount == 0) {
            notv.setVisibility(View.VISIBLE);
        } else {
            notv.setVisibility(View.GONE);
        }
        return iCount;
    }


    class CartHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.ica_check_iv)
        ImageView icaCheckIv;
        @BindView(R.id.ica_shopname_tv)
        TextView icaShopnameTv;
        @BindView(R.id.ica_rv)
        RecyclerView icaRv;

        CartHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }




}
